# -*- coding: utf-8 -*- 
"""
========================================================================================================================
@project : my-sanic
@file: static
@Author: mengying
@email: 652044581@qq.com
@date: 2023/3/7 13:49
@desc: 
========================================================================================================================
"""
from sanic.views import HTTPMethodView
from utils.myDecorator import syncInterface
from utils.myLog import SeqLog
from utils.myCache import Cache
from utils.myResFormat import  ResultJson, ResultCode
from sanic.response import json as JsonResponse

# 配置日志
seqLog = SeqLog().get_logger()

# 配置缓存
RedisClient = Cache().select('redis').current_db

# 数据库导入
from database.model.demo import StudentDao2


# 类试图
class SyncTestView(HTTPMethodView):

    # 同步方法装饰器
    @syncInterface()
    def get(self, request):
        # 方法一: 从app中获取cache实例
        cache = request.app.ctx.cache

        # 本地缓存(默认的使用本地缓存)
        cache.current_db.set("name", "age")
        key = cache.current_db.get("name")
        print(key)

        # 方法二:在py中声明全局实例
        RedisClient.set("name3", "age3")
        key = RedisClient.get("name3")
        print(key)

        # 数据库的使用
        id = StudentDao2().insert_one({"name": "sync"})

        return JsonResponse(ResultJson(ret=ResultCode.SUCCESS, data=id).result)

